package com.zmn.oms.task.job.discount;

import com.alibaba.fastjson.JSON;
import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.spring.tasktracker.JobRunnerItem;
import com.github.ltsopensource.spring.tasktracker.LTS;
import com.github.ltsopensource.tasktracker.Result;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.manager.redis.RedisManager;
import com.zmn.oms.common.constant.RedisKeyConsts;
import com.zmn.oms.manager.utils.TaskConsts;
import com.zmn.oms.model.dto.work.modify.amount.ZsDiscountDTO;
import com.zmn.oms.zmn.business.interfaces.work.masterwork.ZsMasterWorkBService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 类描述： 工程师申请优惠定时
 *
 * @author yule
 * @date 2020/12/9 20:52
 */
@Slf4j
@LTS
public class OrderMasterApplyDiscountJob {
    @Autowired
    private ZsMasterWorkBService zsMasterWorkBService;
    @Autowired
    private RedisManager redisManager;

    @JobRunnerItem(shardValue = TaskConsts.TASK_ID_MASTER_APPLY_DISCOUNT)
    public Result masterApplyDiscountJob(Job job) {
        log.info("开始执行任务消息start -->oms_master_apply_discount_job--------");
        try {
            String applyDiscountJsonStr = job.getParam("applyDiscountDIO");
            ZsDiscountDTO zsDiscountDTO = JSON.parseObject(applyDiscountJsonStr, ZsDiscountDTO.class);
            zsMasterWorkBService.saveDiscount(zsDiscountDTO);

            log.info("orderId={},申请优惠成功,执行完成", zsDiscountDTO.getOrderId());
            String redisKey = String.format(RedisKeyConsts.MASTER_APPLY_DISCOUNT_REVIEWING_KEY, String.valueOf(zsDiscountDTO.getOrderId()), zsDiscountDTO.getMasterId());
            redisManager.del(redisKey);

            return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");

        } catch (Exception ex) {
            log.error(String.format("订单号，申请优惠异常了:%s", ex.getMessage()), ex);
            return new Result(Action.EXECUTE_FAILED, "申请优惠失败");
        }
    }
}
